package com.freemyleft.left.zapp.net.rx;

/**
 * Created by FreeMyleft on 2017/12/1.
 */

import android.content.Context;

import com.freemyleft.left.zapp.net.HttpMethod;
import com.freemyleft.left.zapp.net.RestCreator;
import com.freemyleft.left.zapp.ui.LeftLoader;
import com.freemyleft.left.zapp.ui.LoaderStyle;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * 这个RestClient在每次Buider的时候都会生成一个全新的实例，而这里面的参数是一次构建完毕绝不允许更改的
 */
public class RxRestClient {
    private final String URL;
    private final Map<String, Object> PARAMS  = new HashMap<>();
    private final RequestBody BODY;
    private final File FILE;
    private LoaderStyle LOADER_STYEL;
    private final Context CONTEXT;

    public RxRestClient(String url, Map<String, Object> params,
                        RequestBody body,
                        Context context, LoaderStyle loaderStyle,
                        File file) {

        //bucheng
        this.URL = url;
        PARAMS.putAll(params);
        this.BODY = body;
        this.FILE = file;
        this.CONTEXT = context;
        this.LOADER_STYEL = loaderStyle;

    }

    // 创建创建者
    public static RxRestClentBuilder builder() {
        return new RxRestClentBuilder();

    }

    private Observable<String> request(HttpMethod method) {

        final RxRestService service = RestCreator.getRxRestService();
        Observable<String> observable = null;


        if (LOADER_STYEL != null) {
            LeftLoader.showLoading(CONTEXT, LOADER_STYEL);
        }
        switch (method) {
            case GET:
                observable = service.get(URL, PARAMS);
                break;
            case POST:
                observable = service.post(URL, PARAMS);
                break;
            case POST_RAW:
                observable = service.postRaw(URL, BODY);
                break;
            case PUT:
                observable = service.put(URL, PARAMS);
                break;
            case PUT_RAW:
                observable = service.putRaw(URL, BODY);

            case DELETE:
                observable = service.delete(URL, PARAMS);
                break;
            case UPLOAD:
                final RequestBody requestBody =
                        RequestBody.create(MediaType.parse(MultipartBody.FORM.toString()), FILE);
                final MultipartBody.Part body =
                        MultipartBody.Part.createFormData("file", FILE.getName(), requestBody);

                observable = service.upload(URL, body);
                break;
            default:
                break;
        }
        //Retrofit给我们提供了两个请求的方法 exqueue这是在主线程请求 enqueue在后台另起一个线程并不会影响到我们的UI线程

        return observable;
    }



    public final Observable<String> get() {
        return request(HttpMethod.GET);
    }

    public final Observable<String> post() {
        if (BODY == null) {
            return request(HttpMethod.POST);
        } else {
            if (!PARAMS.isEmpty()) {
                throw new RuntimeException("参数不能为空");
            }
            return request(HttpMethod.POST_RAW);
        }
    }

    public final Observable<String> put() {
        if (BODY == null) {
            return request(HttpMethod.PUT);
        } else {
            if (!PARAMS.isEmpty()) {
                throw new RuntimeException("参数不能为空");
            }
            return request(HttpMethod.PUT_RAW);
        }
    }

    public final Observable<String> delete() {
        return request(HttpMethod.DELETE);
    }

    public final Observable<String> upload() {
        return request(HttpMethod.UPLOAD);
    }

    public final Observable<ResponseBody> download() {
        final Observable<ResponseBody> responseBodyObservable = RestCreator.getRxRestService().download(URL, PARAMS);
        return responseBodyObservable;
    }

}
